<template>
  <div class="raider">
    <!-- 侧边菜单 -->
    <sideMenu @sideChange="changeCity" />
    <!-- 主体列表 -->
    <div class="mainList">
      <!-- 搜索框 -->
      <div class="searchBox">
        <input
          type="text"
          placeholder="请输入想去的地方，比如：广州"
          v-model="cityText"
          @keyup.enter="searchCity"
        />
        <span class="iconfont icon-research" @click="searchCity"></span>
      </div>
      <!-- 搜索推荐 -->
      <div class="searchRecommend">
        推荐：
        <span
          v-for="(item,index) in recommendCity"
          :key="index"
          @click="changeCity(item)"
        >{{item}}</span>
      </div>
      <!-- 列表头部 -->
      <div class="raiderHeader">
        <h4>推荐攻略</h4>
        <el-button type="primary" icon="el-icon-edit" @click="$router.push('/raider/create')">写游记</el-button>
      </div>
      <!-- 推荐文章列表 -->
      <postList :data="article.data" />
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="article.total"
        v-if="showPage"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import sideMenu from "@/components/raider/sideMenu";
import postList from "@/components/raider/postList";
export default {
  components: {
    postList,
    sideMenu,
  },
  data() {
    return {
      cityText: null,
      article: {},
      pageSize: 3,
      currentPage: 1,
      recommendCity: ["广州", "上海", "北京"],
      showPage: true,
    };
  },
  created() {
    if (this.$route.query.city) {
      this.cityText = this.$route.query.city;
    }
    this.searchCity();
  },
  //可以进行seo优化，这只做title变化
  head() {
    return {
      title: "旅游攻略",
    };
  },
  watch: {
    article() {
      if (this.article.data.length == 0) {
        this.showPage = false;
      } else {
        this.showPage = true;
      }
    },
  },
  methods: {
    //搜索城市攻略
    searchCity() {
      var obj = {
        _start: (this.currentPage - 1) * this.pageSize,
        _limit: this.pageSize,
      };
      if (this.cityText) {
        obj.city = this.cityText;
      }
      this.$axios({
        url: "/posts",
        params: obj,
      }).then((res) => {
        this.article = res.data;
      });
      this.$router.push({ query: obj });
    },
    //页面显示条数
    handleSizeChange(val) {
      this.pageSize = val;
      this.searchCity();
    },
    //改变当前页码
    handleCurrentChange(val) {
      this.currentPage = val;
      this.searchCity();
    },
    //改变城市搜索
    changeCity(item) {
      this.cityText = item;
      this.searchCity();
    },
  },
};
</script>

<style lang="less" scoped>
.raider {
  width: 1000px;
  margin: 0 auto;
  padding: 20px 0;
  display: flex;
  justify-content: space-between;

  .mainList {
    width: 700px;
    .searchBox {
      width: 100%;
      height: 40px;
      line-height: 40px;
      border: 3px solid #ffa500;
      display: flex;
      align-items: center;
      input {
        flex: 1;
        padding: 0 10px;
        color: #666;
      }
      .icon-research {
        font-size: 24px;
        color: #ffa500;
        font-weight: 700;
        margin-right: 10px;
      }
    }
    .searchRecommend {
      padding: 10px 0;
      font-size: 12px;
      color: #666;
      cursor: pointer;
      span {
        margin: 0 5px;
        &:hover {
          color: #ffa500;
          text-decoration: underline;
        }
      }
    }
    .raiderHeader {
      display: flex;
      justify-content: space-between;
      border-bottom: 1px solid #ddd;
      padding: 10px 0;
      align-items: center;
      position: relative;
      h4 {
        font-size: 18px;
        color: #ffa500;
        font-weight: 400;
        &::after {
          content: "";
          display: block;
          height: 2px;
          width: 72px;
          position: absolute;
          left: 0;
          bottom: -1px;
          background: #ffa500;
        }
      }
    }
    .el-pagination {
      text-align: center;
      margin: 10px 0;
    }
  }
}
</style>